package common;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.nio.MappedByteBuffer;
import java.util.ArrayList;
import java.util.regex.Pattern;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import client.Client;

/*
 * This is to draw the GUI (the game map) 
 */


public class Map extends JFrame implements ActionListener,Runnable {

	JPanel area1=new JPanel();  
	JPanel area2=new JPanel();
	JPanel area3=new JPanel();
	JPanel area4=new JPanel();
	String[][] mapp=new String[6][6];
	String[][] otherMap;
	Client client=new Client();
	int row;
	int col;
	ArrayList buttonList=new ArrayList();
	int s=0;
	int d=0;
	int b=0;
	int c=0;

	int s1=0;
	int d1=0;
	int b1=0;
	int c1=0;

	ArrayList<String> bb=new ArrayList<String>();
	ArrayList<String> cc=new ArrayList<String>();
	ArrayList<String> dd=new ArrayList<String>();
	ArrayList<String> ss=new ArrayList<String>();

	ArrayList<String> bb1=new ArrayList<String>();
	ArrayList<String> cc1=new ArrayList<String>();
	ArrayList<String> dd1=new ArrayList<String>();
	ArrayList<String> ss1=new ArrayList<String>();



	JLabel topL=new JLabel("Battleship  Game");
	JLabel playerA=new JLabel("playerA   Battleship");
	JLabel playerB=new JLabel("Game   playerB");
	private JLabel middleL=new JLabel();
	String flag;


	JButton b11=new JButton();
	JButton b12=new JButton();
	JButton b13=new JButton();
	JButton b14=new JButton();
	JButton b15=new JButton();
	JButton b16=new JButton();
	JButton b21=new JButton();
	JButton b22=new JButton();
	JButton b23=new JButton();
	JButton b24=new JButton();
	JButton b25=new JButton();
	JButton b26=new JButton();
	JButton b31=new JButton();
	JButton b32=new JButton();
	JButton b33=new JButton();
	JButton b34=new JButton();
	JButton b35=new JButton();
	JButton b36=new JButton();
	JButton b41=new JButton();
	JButton b42=new JButton();
	JButton b43=new JButton();
	JButton b44=new JButton();
	JButton b45=new JButton();
	JButton b46=new JButton();
	JButton b51=new JButton();
	JButton b52=new JButton();
	JButton b53=new JButton();
	JButton b54=new JButton();
	JButton b55=new JButton();
	JButton b56=new JButton();
	JButton b61=new JButton();
	JButton b62=new JButton();
	JButton b63=new JButton();
	JButton b64=new JButton();
	JButton b65=new JButton();
	JButton b66=new JButton();


	JButton aa11=new JButton();
	JButton aa12=new JButton();
	JButton aa13=new JButton();
	JButton aa14=new JButton();
	JButton aa15=new JButton();
	JButton aa16=new JButton();
	JButton bb21=new JButton();
	JButton bb22=new JButton();
	JButton bb23=new JButton();
	JButton bb24=new JButton();
	JButton bb25=new JButton();
	JButton bb26=new JButton();
	JButton cc31=new JButton();
	JButton cc32=new JButton();
	JButton cc33=new JButton();
	JButton cc34=new JButton();
	JButton cc35=new JButton();
	JButton cc36=new JButton();
	JButton dd41=new JButton();
	JButton dd42=new JButton();
	JButton dd43=new JButton();
	JButton dd44=new JButton();
	JButton dd45=new JButton();
	JButton dd46=new JButton();
	JButton ee51=new JButton();
	JButton ee52=new JButton();
	JButton ee53=new JButton();
	JButton ee54=new JButton();
	JButton ee55=new JButton();
	JButton ee56=new JButton();
	JButton ff61=new JButton();
	JButton ff62=new JButton();
	JButton ff63=new JButton();
	JButton ff64=new JButton();
	JButton ff65=new JButton();
	JButton ff66=new JButton();
	JButton[][] button=new JButton[6][6];
	JButton[][] otherButton=new JButton[6][6];




	public Map() {
		super();


		Container container=getContentPane();

		button[0][0]=b11;
		button[0][1]=b12;
		button[0][2]=b13;
		button[0][3]=b14;
		button[0][4]=b15;
		button[0][5]=b16;
		button[1][0]=b21;
		button[1][1]=b22;
		button[1][2]=b23;
		button[1][3]=b24;
		button[1][4]=b25;
		button[1][5]=b26;
		button[2][0]=b31;
		button[2][1]=b32;
		button[2][2]=b33;
		button[2][3]=b34;
		button[2][4]=b35;
		button[2][5]=b36;
		button[3][0]=b41;
		button[3][1]=b42;
		button[3][2]=b43;
		button[3][3]=b44;
		button[3][4]=b45;
		button[3][5]=b46;
		button[4][0]=b51;
		button[4][1]=b52;
		button[4][2]=b53;
		button[4][3]=b54;
		button[4][4]=b55;
		button[4][5]=b56;
		button[5][0]=b61;
		button[5][1]=b62;
		button[5][2]=b63;
		button[5][3]=b64;
		button[5][4]=b65;
		button[5][5]=b66;



		otherButton[0][0]=aa11;
		otherButton[0][1]=aa12;
		otherButton[0][2]=aa13;
		otherButton[0][3]=aa14;
		otherButton[0][4]=aa15;
		otherButton[0][5]=aa16;
		otherButton[1][0]=bb21;
		otherButton[1][1]=bb22;
		otherButton[1][2]=bb23;
		otherButton[1][3]=bb24;
		otherButton[1][4]=bb25;
		otherButton[1][5]=bb26;
		otherButton[2][0]=cc31;
		otherButton[2][1]=cc32;
		otherButton[2][2]=cc33;
		otherButton[2][3]=cc34;
		otherButton[2][4]=cc35;
		otherButton[2][5]=cc36;
		otherButton[3][0]=dd41;
		otherButton[3][1]=dd42;
		otherButton[3][2]=dd43;
		otherButton[3][3]=dd44;
		otherButton[3][4]=dd45;
		otherButton[3][5]=dd46;
		otherButton[4][0]=ee51;
		otherButton[4][1]=ee52;
		otherButton[4][2]=ee53;
		otherButton[4][3]=ee54;
		otherButton[4][4]=ee55;
		otherButton[4][5]=ee56;
		otherButton[5][0]=ff61;
		otherButton[5][1]=ff62;
		otherButton[5][2]=ff63;
		otherButton[5][3]=ff64;
		otherButton[5][4]=ff65;
		otherButton[5][5]=ff66;





		area1.add(b11);
		area1.add(b12);
		area1.add(b13);
		area1.add(b14);
		area1.add(b15);
		area1.add(b16);
		area1.add(b21);
		area1.add(b22);
		area1.add(b23);
		area1.add(b24);
		area1.add(b25);
		area1.add(b26);
		area1.add(b31);
		area1.add(b32);
		area1.add(b33);
		area1.add(b34);
		area1.add(b35);
		area1.add(b36);
		area1.add(b41);
		area1.add(b42);
		area1.add(b43);
		area1.add(b44);
		area1.add(b45);
		area1.add(b46);
		area1.add(b51);
		area1.add(b52);
		area1.add(b53);
		area1.add(b54);
		area1.add(b55);
		area1.add(b56);
		area1.add(b61);
		area1.add(b62);
		area1.add(b63);
		area1.add(b64);
		area1.add(b65);
		area1.add(b66);

		area2.add(aa11);
		area2.add(aa12);
		area2.add(aa13);
		area2.add(aa14);
		area2.add(aa15);
		area2.add(aa16);
		area2.add(bb21);
		area2.add(bb22);
		area2.add(bb23);
		area2.add(bb24);
		area2.add(bb25);
		area2.add(bb26);
		area2.add(cc31);
		area2.add(cc32);
		area2.add(cc33);
		area2.add(cc34);
		area2.add(cc35);
		area2.add(cc36);
		area2.add(dd41);
		area2.add(dd42);
		area2.add(dd43);
		area2.add(dd44);
		area2.add(dd45);
		area2.add(dd46);
		area2.add(ee51);
		area2.add(ee52);
		area2.add(ee53);
		area2.add(ee54);
		area2.add(ee55);
		area2.add(ee56);
		area2.add(ff61);
		area2.add(ff62);
		area2.add(ff63);
		area2.add(ff64);
		area2.add(ff65);
		area2.add(ff66);

		area1.setLayout(new GridLayout(6, 6));
		area2.setLayout(new GridLayout(6, 6));

		area4.add(playerA);
		area4.add(playerB);
		area3.add(area4);
		area3.add(middleL);
		area3.setLayout(new GridLayout(2, 6));

		container.add(area1);
		container.add(area3);
		container.add(area2);
		container.setLayout((new GridLayout(1, 3)));
		for(int i=1;i<7;i++){
			for(int j=1;j<7;j++){
				String name;
				name="b"+"";
			}
		}
		aa11.addActionListener(this);
		aa12.addActionListener(this);
		aa13.addActionListener(this);
		aa14.addActionListener(this);
		aa15.addActionListener(this);
		aa16.addActionListener(this);
		bb21.addActionListener(this);
		bb22.addActionListener(this);
		bb23.addActionListener(this);
		bb24.addActionListener(this);
		bb25.addActionListener(this);
		bb26.addActionListener(this);
		cc31.addActionListener(this);
		cc32.addActionListener(this);
		cc33.addActionListener(this);
		cc34.addActionListener(this);
		cc35.addActionListener(this);
		cc36.addActionListener(this);
		dd41.addActionListener(this);
		dd42.addActionListener(this);
		dd43.addActionListener(this);
		dd44.addActionListener(this);
		dd45.addActionListener(this);
		dd46.addActionListener(this);
		ee51.addActionListener(this);
		ee52.addActionListener(this);
		ee53.addActionListener(this);
		ee54.addActionListener(this);
		ee55.addActionListener(this);
		ee56.addActionListener(this);
		ff61.addActionListener(this);
		ff62.addActionListener(this);
		ff63.addActionListener(this);
		ff64.addActionListener(this);
		ff65.addActionListener(this);
		ff66.addActionListener(this);
	}


	public void setOwnShip(String[][] mapp ){

		b11.setLabel(mapp[0][0]);
		b12.setLabel(mapp[0][1]);
		b13.setLabel(mapp[0][2]);
		b14.setLabel(mapp[0][3]);
		b15.setLabel(mapp[0][4]);
		b16.setLabel(mapp[0][5]);
		b21.setLabel(mapp[1][0]);
		b22.setLabel(mapp[1][1]);
		b23.setLabel(mapp[1][2]);
		b24.setLabel(mapp[1][3]);
		b25.setLabel(mapp[1][4]);
		b26.setLabel(mapp[1][5]);
		b31.setLabel(mapp[2][0]);
		b32.setLabel(mapp[2][1]);
		b33.setLabel(mapp[2][2]);
		b34.setLabel(mapp[2][3]);
		b35.setLabel(mapp[2][4]);
		b36.setLabel(mapp[2][5]);
		b41.setLabel(mapp[3][0]);
		b42.setLabel(mapp[3][1]);
		b43.setLabel(mapp[3][2]);
		b44.setLabel(mapp[3][3]);
		b45.setLabel(mapp[3][4]);
		b46.setLabel(mapp[3][5]);
		b51.setLabel(mapp[4][0]);
		b52.setLabel(mapp[4][1]);
		b53.setLabel(mapp[4][2]);
		b54.setLabel(mapp[4][3]);
		b55.setLabel(mapp[4][4]);
		b56.setLabel(mapp[4][5]);
		b61.setLabel(mapp[5][0]);
		b62.setLabel(mapp[5][1]);
		b63.setLabel(mapp[5][2]);
		b64.setLabel(mapp[5][3]);
		b65.setLabel(mapp[5][4]);
		b66.setLabel(mapp[5][5]);



	}

	public void setFlag(String str){
		flag=str;
	}


	public void setOtherMap(String[][] otherMapp){

		aa11.setLabel(otherMap[0][0]);
		aa12.setLabel(otherMap[0][1]);
		aa13.setLabel(otherMap[0][2]);
		aa14.setLabel(otherMap[0][3]);
		aa15.setLabel(otherMap[0][4]);
		aa16.setLabel(otherMap[0][5]);
		bb21.setLabel(otherMap[1][0]);
		bb22.setLabel(otherMap[1][1]);
		bb23.setLabel(otherMap[1][2]);
		bb24.setLabel(otherMap[1][3]);
		bb25.setLabel(otherMap[1][4]);
		bb26.setLabel(otherMap[1][5]);
		cc31.setLabel(otherMap[2][0]);
		cc32.setLabel(otherMap[2][1]);
		cc33.setLabel(otherMap[2][2]);
		cc34.setLabel(otherMap[2][3]);
		cc35.setLabel(otherMap[2][4]);
		cc36.setLabel(otherMap[2][5]);
		dd41.setLabel(otherMap[3][0]);
		dd42.setLabel(otherMap[3][1]);
		dd43.setLabel(otherMap[3][2]);
		dd44.setLabel(otherMap[3][3]);
		dd45.setLabel(otherMap[3][4]);
		dd46.setLabel(otherMap[3][5]);
		ee51.setLabel(otherMap[4][0]);
		ee52.setLabel(otherMap[4][1]);
		ee53.setLabel(otherMap[4][2]);
		ee54.setLabel(otherMap[4][3]);
		ee55.setLabel(otherMap[4][4]);
		ee56.setLabel(otherMap[4][5]);
		ff61.setLabel(otherMap[5][0]);
		ff62.setLabel(otherMap[5][1]);
		ff63.setLabel(otherMap[5][2]);
		ff64.setLabel(otherMap[5][3]);
		ff65.setLabel(otherMap[5][4]);
		ff66.setLabel(otherMap[5][5]);

	}




	/*
	 * This method is to listen on the button
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */

	public  void actionPerformed(ActionEvent e){

		// TODO Auto-generated method stub

		if(e.getSource().equals(aa11)){
			row=1;
			col=1;
		}else if(e.getSource().equals(aa12)){
			row=1;
			col=2;
		}else if(e.getSource().equals(aa13)){
			row=1;
			col=3;
		}
		else if(e.getSource().equals(aa14)){
			row=1;
			col=4;
		}
		else if(e.getSource().equals(aa15)){
			row=1;
			col=5;
		}
		else if(e.getSource().equals(aa16)){
			row=1;
			col=6;
		}
		else if(e.getSource().equals(bb21)){
			row=2;
			col=1;
		}
		else if(e.getSource().equals(bb22)){
			row=2;
			col=2;
		}
		else if(e.getSource().equals(bb23)){
			row=2;
			col=3;
		}
		else if(e.getSource().equals(bb24)){
			row=2;
			col=4;
		}
		else if(e.getSource().equals(bb25)){
			row=2;
			col=5;
		}
		else if(e.getSource().equals(bb26)){
			row=2;
			col=6;
		}
		else if(e.getSource().equals(cc31)){
			row=3;
			col=1;
		}else if(e.getSource().equals(cc32)){
			row=3;
			col=2;
		}
		else if(e.getSource().equals(cc33)){
			row=3;
			col=3;
		}
		else if(e.getSource().equals(cc34)){
			row=3;
			col=4;
		}
		else if(e.getSource().equals(cc35)){
			row=3;
			col=5;
		}
		else if(e.getSource().equals(cc36)){
			row=3;
			col=6;
		}
		else if(e.getSource().equals(dd41)){
			row=4;
			col=1;
		}else if(e.getSource().equals(dd42)){
			row=4;
			col=2;
		}
		else if(e.getSource().equals(dd43)){
			row=4;
			col=3;
		}
		else if(e.getSource().equals(dd44)){
			row=4;
			col=4;
		}
		else if(e.getSource().equals(dd45)){
			row=4;
			col=5;
		}
		else if(e.getSource().equals(dd46)){
			row=4;
			col=6;
		}	else if(e.getSource().equals(ee51)){
			row=5;
			col=1;
		}else if(e.getSource().equals(ee52)){
			row=5;
			col=2;
		}
		else if(e.getSource().equals(ee53)){
			row=5;
			col=3;
		}
		else if(e.getSource().equals(ee54)){
			row=5;
			col=4;
		}
		else if(e.getSource().equals(ee55)){
			row=5;
			col=5;
		}
		else if(e.getSource().equals(ee56)){
			row=5;
			col=6;
		}
		else if(e.getSource().equals(ff61)){
			row=6;
			col=1;
		}else if(e.getSource().equals(ff62)){
			row=6;
			col=2;
		}
		else if(e.getSource().equals(ff63)){
			row=6;
			col=3;
		}
		else if(e.getSource().equals(ff64)){
			row=6;
			col=4;
		}
		else if(e.getSource().equals(ff65)){
			row=6;
			col=5;
		}
		else if(e.getSource().equals(ff66)){
			row=6;
			col=6;
		}

		try {

			if(flag.equals("1")&&PressAgain()){
				flag="0";
				middleL.setText("");
				System.out.print("++++++++++++++++++");
				client.send(Integer.toString(row-1)+Integer.toString(col-1));
				System.out.print("------------------");
				buttonList.add(Integer.toString(row)+Integer.toString(col));
			}



		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} catch (ClassNotFoundException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}

	}


	public void builtMap(String [][] a) throws IOException, ClassNotFoundException{
		setOwnShip(a);
	}

	public synchronized void updateMap(String str) throws InterruptedException{
		String[] strr=processMsg(str);

		if(strr.length==4){
			if(strr[3].equals("1")){
				System.out.println("change button upcase");
				b++;
				bb.add(str);
				if(b==4){
					for(int i=1;i<5;i++){
						String[] spilt=processMsg(bb.get(i-1));
						otherButton[Integer.parseInt(spilt[1])][Integer.parseInt(spilt[2])].setLabel("B");
					}
				}else{
					otherButton[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("X");		
				}

			}else if(strr[3].equals("2")){
				c++;	
				cc.add(str);
				if(c==3){
					for(int i=1;i<4;i++){
						String[] spilt=processMsg(cc.get(i-1));
						otherButton[Integer.parseInt(spilt[1])][Integer.parseInt(spilt[2])].setLabel("C");
					}
				}else{

					otherButton[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("X");
				}

			}else if(strr[3].equals("3")){
				d++;
				dd.add(str);
				if(d==2){
					for(int i=1;i<3;i++){
						String[] spilt=processMsg(dd.get(i-1));
						otherButton[Integer.parseInt(spilt[1])][Integer.parseInt(spilt[2])].setLabel("D");
					}
				}else{

					otherButton[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("X");
				}

			}else if(strr[3].equals("4")){
				otherButton[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("S");
			}else if(strr[3].equals("0")){
				otherButton[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("/");
			}



		}else if(strr.length==5){

			if(strr[3].equals("1")){
				System.out.println("change button upcase");
				b1++;
				bb1.add(str);
				if(b1==4){
					for(int i=1;i<5;i++){
						String[] spilt=processMsg(bb1.get(i-1));
						button[Integer.parseInt(spilt[1])][Integer.parseInt(spilt[2])].setLabel("B");
					}
				}else{
					button[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("X");		
				}
			}else if(strr[3].equals("2")){
				c1++;	
				cc1.add(str);
				if(c1==3){
					for(int i=1;i<4;i++){
						String[] spilt=processMsg(cc1.get(i-1));
						button[Integer.parseInt(spilt[1])][Integer.parseInt(spilt[2])].setLabel("C");
					}
				}else{

					button[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("X");
				}

			}else if(strr[3].equals("3")){
				d1++;
				dd1.add(str);
				if(d1==2){
					for(int i=1;i<3;i++){
						String[] spilt=processMsg(dd1.get(i-1));
						button[Integer.parseInt(spilt[1])][Integer.parseInt(spilt[2])].setLabel("D");
					}
				}
				else{

					button[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("X");
				}
			}
			else if(strr[3].equals("4")){
				button[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("S");
			}else if(strr[3].equals("0")){
				button[Integer.parseInt(strr[1])][Integer.parseInt(strr[2])].setLabel("/");
			}
		}
		else if(strr.length==2){
			if(strr[1].equals("1")){
				flag="1";
				System.out.print("*********************");
				middleL.setText("                         YOUR TURN");
			}else if (strr[1].equals("0")){
				flag="0";
				//middleL.setText("                  not your turn");
			}else if(strr[1].equals("9")){
				middleL.setText("               YOU ARE WINNER");
			}


		}
	}

	public String[] processMsg(String str){
		String[] r = str.split("");
		return r;
	}



	public boolean PressAgain() throws IOException, ClassNotFoundException{
		String str=Integer.toString(row)+Integer.toString(col);
		if(buttonList.contains(str)){
			middleL.setText("            pressed!press other button");
			return false;
		}else{
			return true;
		}
	}




	public static void main(String[] args) throws UnknownHostException, IOException{
		Map map1=new Map();
		Thread playa=new Thread(map1);
		Map map2=new Map();
		Thread playb=new Thread(map2);
		playa.setName("a");
		playb.setName("b");
		playa.start();
		playb.start();
	}


	@Override
	public void run() {
		// TODO Auto-generated method stub
		try {
			int i=0;


			while(i<1){
				client.run();
				builtMap(client.receive());
				//mapp=client.receive();
				setSize(800,360);
				setVisible(true);
				i++;
			}
			while(Constants.isRunning){
				updateMap(client.receiveNormal());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}





/*public static void main(String[] args){
		Map map=new Map();
		setOwnShip(mapp);
		setOtherMap(otherMap);


	}*/


